package com.hengtian.roastwall.controller;

import com.hengtian.roastwall.common.ObjectRestResponse;
import com.hengtian.roastwall.entity.User;
import com.hengtian.roastwall.entity.dto.RegisterDto;
import com.hengtian.roastwall.service.UserService;
import com.hengtian.roastwall.service.impl.MomentServiceImpl;
import com.hengtian.roastwall.utils.RedisCache;
import com.hengtian.roastwall.utils.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

@Slf4j
@RestController
@RequestMapping("/user")
@Api("用户接口")
public class UserController {
    @Autowired
    private UserService userService;


    @Autowired
    private MomentServiceImpl momentService;

    @ApiOperation("用户登录")
    @PostMapping("/login")
    public ResponseResult login(@RequestBody User user) {
        return userService.login(user);
    }

    @ApiOperation("用户登出")
    @PostMapping("/logout")
    public ResponseResult logout() {
        return userService.logout();
    }

    @ApiOperation("获取用户信息")
    @PostMapping("/getUserInfo")
    public ResponseResult<User> getUserInfo(@RequestBody User user) {
        System.out.println(user.getUserName());
        User userInfo = userService.getUserInfo(user.getUserName());
        return new ResponseResult<User>(200, userInfo);

    }

    @ApiOperation("用户信息更新功能")
    @PostMapping("/avatar")
    public ResponseResult avatarUpload(@RequestBody User user) {
        int count = userService.updateAvatarById(user);

        if(count>0){
            return new ResponseResult(200,"更新成功");
        }else{
            return new ResponseResult(500,"更新失败");
        }
    }

    @ApiOperation("注册功能")
    @PostMapping("/register")
    public ResponseResult<String> register(@RequestBody RegisterDto dto) {
        User user = userService.register(dto);
        ResponseResult<String> result = new ResponseResult<>(user == null ? 200 : 400,user == null ? "success" : "用户名已存在");
        result.setData(user == null ? "success" : "用户名已存在");
        return result;
    }
}
